package org.rcgwt.client.ui.validator.impl;

import java.util.Map;

import org.rcgwt.client.ui.validator.ICharacterSets;
import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.IFilter;
import org.rcgwt.client.util.Pattern;
import org.rcgwt.client.util.PatternBuilder;

/**
 * A filter which filters an hour character
 * 
 * @author Elian ORIOU
 * 
 */

public class HourFilter implements IFilter {

	private final String MY_PATTERN = ICharacterSets.NUM_SET;
	private final Pattern AMPM_PATTERN;
	private String sepSign;

	public HourFilter(Map<String, String> parameters) {
		AMPM_PATTERN = Pattern.compile("[APM]");
		/* Default Separator-sign */
		sepSign = ":";
		// Separator Sign parameters
		if (parameters.containsKey("hour.sepSign")) {
			sepSign = PatternBuilder.encode("%%1", parameters
					.get("hour.sepSign"));
		}
	}

	@Override
	public boolean filter(IClientValidatorContext context, char c) {
		/*
		 * If the character is a digit, a specified separator sign, or an
		 * (antemeridiem|postmeridiem)
		 */
		if (MY_PATTERN.indexOf(c) != -1 || c == sepSign.charAt(0)
				|| AMPM_PATTERN.matches(String.valueOf(c))) {
			return true;
		}
		return false;
	}
}
